﻿<template>
	<div class="equipInfo-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="queryParams" ref="queryForm" :inline="true">
			<el-row :gutter="35">
              <el-col :xs="24" :sm="12" :md="6" :lg="6" :xl="6" >
              <el-form-item label="关键字">
              <el-input v-model="queryParams.name" clearable placeholder="名称、编号、单元、绑定号、组名、IP"/>
              </el-form-item>
              </el-col>
            
              <el-col :xs="24" :sm="12" :md="4" :lg="4" :xl="4" >
              <el-form-item label="程序">
              

						<el-select v-model="queryParams.programid" class="m-2" placeholder="选择" >
							<el-option v-for="item in equipInfos" :key="item.id" :label="item.name" :value="item.id" />
						</el-select>
				
              </el-form-item>
              </el-col>


           
              <el-col :xs="24" :sm="12" :md="4" :lg="4" :xl="4" >
              <el-form-item label="设备类型">
            

			  <el-select filterable v-model="queryParams.equiptype" class="m-2" placeholder="选择" >
							<el-option v-for="item in equipTypes" :key="item.item1" :label="item.item2" :value="item.item1" />
						</el-select>

              </el-form-item>
              </el-col>

			  <el-col :xs="24" :sm="12" :md="4" :lg="4" :xl="4" >
              <el-form-item label="设备协议">
            

			  <el-select filterable v-model="queryParams.protobufType" class="m-2" placeholder="选择" >
							<el-option v-for="item in equipProtos" :key="item.item1" :label="item.item1" :value="item.item1" />
						</el-select>

              </el-form-item>
              </el-col>
			<el-col :xs="24" :sm="12" :md="8" :lg="4" :xl="4" class="mb20 search-actions">
				<div>
				<el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
				<el-button type="primary" plain icon="ele-Search" @click="handleQuery" v-auth="'equipValue:page'"> 查询 </el-button>
				</div>
			</el-col>
			
			
		
      </el-row>
      </el-form>
		</el-card>
		<el-card shadow="hover" style="margin-top: 8px">
			<el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
				<el-table-column type="index" label="序号" width="55" align="center" fixed="" />
				 <el-table-column prop="name" label="名称" fixed="" show-overflow-tooltip="" />
				
				 <el-table-column prop="programName" label="程序" fixed="" show-overflow-tooltip="" />
			
				 <el-table-column prop="num" label="编号" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="unitNum" label="单元号" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="equipType" label="设备类型" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="protobufType" label="协议类型" fixed="" show-overflow-tooltip="" />
				 <el-table-column  label="运行状态" fixed="" show-overflow-tooltip="" >
					<template #default="scope">
						<el-tag :type="scope.row.status?'success':'danger'" round>{{ scope.row.status?'运行中':'未运行' }}</el-tag>
					</template>
				 </el-table-column>
				 <el-table-column  label="连接状态" fixed="" show-overflow-tooltip="" >
					<template #default="scope">
						<el-tag :type="scope.row.connection?'success':'danger'" round>{{ scope.row.connection?'已连接':'未连接' }}</el-tag>
					</template>
				 </el-table-column>
				 <el-table-column prop="readTime" label="更新时间" fixed="" show-overflow-tooltip="" />
				 <el-table-column prop="value" fixed="" label="值"  show-overflow-tooltip="" />
				 <el-table-column prop="remark" fixed="right" width="140" label="备注" :filters="[
				  	{ text: '异常', value: 1 },				  
				  ]"
	      :filter-method="filterRemark"
	      filter-placement="bottom-end"  show-overflow-tooltip="" />
				<el-table-column label="操作" width="180" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('equipValue:control') ">
					<template #default="scope">
						<el-button size="small" text="" type="primary" @click="runTask(scope.row,2)" v-auth="'equipValue:control'"> 连接 </el-button>
						<el-button  size="small" text="" type="primary" @click="runTask(scope.row,3)" v-auth="'equipValue:control'"> 断开 </el-button>
						<el-button size="small" text="" type="primary" @click="runTask(scope.row,100)" v-auth="'equipValue:control'"> 命令 </el-button>
						<el-button  v-if="scope.row.hasTags" size="small" text="" type="primary" @click="openTags(scope.row)" v-auth="'equipValue:control'"> 点位 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
			<editDialog
			    ref="editDialogRef"
			    :title="editEquipInfoTitle"
			    @reloadTable="handleQuery"
      />
		</el-card>
	</div>
</template>

<script lang="ts" setup>
	import { ref } from "vue";
	import { ElMessageBox, ElMessage } from "element-plus";
	import { auth } from '/@/utils/authFunction';
	//import { formatDate } from '/@/utils/formatTime';

	import editDialog from '/@/views/main/equipInfo/component/editDialog.vue'
	import { getEquipTypes,getProtoTypes } from '/@/api/main/equipInfo';
	import { getEquipValuePageList } from '/@/api/main/equipValue';
	import { getProgramList } from '/@/api/main/programAll';
	import { ExecuteProgram } from '/@/api/main/programAll';

	import {  pageView } from '/@/comps/page/index';
	import { useRoute } from 'vue-router';

	const equipTypes=ref<any>([]);
	const equipProtos=ref<any>([]);
	const equipInfos=ref<any>([]);
	const editDialogRef = ref();
	const loading = ref(false);
	const tableData = ref<any>([]);
	

	const route = useRoute();
	const action = ref("");
	let mode = ref({});
	
	const queryParams = ref<any>({});
	const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
	});
	const editEquipInfoTitle = ref("");


	// 查询操作
	const handleQuery = async () => {
	loading.value = true;
	var res = await getEquipValuePageList(Object.assign(queryParams.value, tableParams.value));
	tableData.value = res.data.result?.items ?? [];
	tableParams.value.total = res.data.result?.total;
	loading.value = false;
};
// 初始化
const iniQuery=async()=>{
	if(route.query.name){
		queryParams.value.name=route.query.name;
		
	}
	loading.value=true;
	var res=await getProgramList();
	equipInfos.value=res.data.result;
	var rep=await getEquipTypes();
	equipTypes.value=rep.data.result;

	var result=await getProtoTypes();
	equipProtos.value=result.data.result;

	loading.value=false;

	
}

// 打开点位页面
const openTags = (row:any) => {
	pageView(`/#/iotsetting/tagdata?id=${row.id}&protobufType=${row.protobufType}`);
};

const runTask = (row: any,type:number) => {
  ElMessageBox.confirm(`确定要执行该操作吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
		
	    let command={programId:row.programId,equipId:row.id,controlType:type,protobufType:row.protobufType};
	    await ExecuteProgram(command);
      handleQuery();
      ElMessage.success("执行成功");
    })
    .catch(() => {});
};

const filterRemark = (value: number, row: any) => {
  if(value==1){
	return !row.connection && row.remark && row.remark.length>0;
  }
  else return true;
}

// 改变页面容量
const handleSizeChange = (val: number) => {
  tableParams.value.pageSize = val;
  handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
  tableParams.value.page = val;
  handleQuery();
};
 iniQuery();
 handleQuery();


</script>


